from django.shortcuts import render
from django.shortcuts import HttpResponse, redirect

from bookmanage.models import Book, Author
import decimal
from django.core.paginator import Paginator


# Create your views here.
def addbook(request):
    # name = request.GET.get("name", "")
    # price = request.GET.get("price", "0.0")
    # isbn_no = request.GET.get("isbn_no", "")
    # enterperson = request.GET.get("enterperson", "")

    name = request.POST.get("name", "")
    price = request.POST.get("price", "0.0")
    isbn_no = request.POST.get("isbn_no", "")
    enterperson = request.POST.get("enterperson", "")

    book = Book(name=name, price=decimal.Decimal(float(price)), isbn_no=isbn_no, enterperson=enterperson)
    # book = Book(name=name, price=decimal.Decimal(price), isbn_no=isbn_no)
    book.save()
    # Book.objects.aggregate()
    # return redirect("/bookai/showbooks/")
    # return redirect("showbooks", permanent=True)
    # return HttpResponse("添加成功")
    return redirect("showbooks", permanent=True)


def showbook(request, bookid):
    book = []
    try:
        book = Book.objects.get(id=bookid)
        print(Book.objects.all().query)
        print(type(Book.objects.all().raw()))
    except:
        print("---")

    if book:
        return render(request, "book.html", {"book": book})
    else:
        return HttpResponse("查无此书,请联系110索取")


def delbook(request, bookid):
    book = ""
    try:
        book = Book.objects.get(id=bookid)
    except:
        pass
    if book:
        book.delete()
        # return redirect("/bookai/showbooks/",permanent=True)
        return redirect("showbooks", permanent=True)
    else:
        return HttpResponse("没有这本书!")


def homepage(request):
    # return render(request, "homepage.html")
    return render(request, "shange.html")


# def bookinfocollect(request):
#     return render(request, "book_info_collect.html")


def bookinfocollect(request):
    if request.method == "GET":
        return render(request, "book_info_collect.html")
    elif request.method == "POST":
        name = request.POST.get("name", "")
        price = request.POST.get("price", "0.0")
        isbn_no = request.POST.get("isbn_no", "")
        enterperson = request.POST.get("enterperson", "")

        book = Book(name=name, price=decimal.Decimal(float(price)), isbn_no=isbn_no, enterperson=enterperson)
        book.save()
        # return HttpResponse("添加成功")
        return redirect("showbooks", permanent=True)


def showbooks(request):
    # orderbyname = 1  根据name 正向排序
    # orderbyname = 0  根据name 反向排序
    # 查询出所有书籍
    # books = Book.objects.all().order_by("-price")
    orderbyname = request.GET.get("orderbyname", "1")
    # print(type(orderbyname))
    # "0"
    if int(orderbyname):
        books = Book.objects.all().order_by("name")
    else:
        books = Book.objects.all().order_by("-name")

    page = request.GET.get("page", "1")
    page = int(page)
    # todo 分页
    p = Paginator(books, 3)
    current_page = p.get_page(page)
    pre_page = current_page.number - 1
    next_page = current_page.number + 1
    maxpage = p.num_pages

    # 判断有没有下一页
    shownext = True
    if current_page.number > maxpage - 1:
        shownext = False
        current_page.number = maxpage

    # todo 其他写法
    return render(request, "showbooks.html", {"books": current_page.object_list,
                                              "pre_page": pre_page,
                                              "current_page_num": current_page.number,
                                              "next_page": next_page,
                                              "shownext": shownext})


def updatebook(request, bookid):
    # 查询出所有书籍
    book = Book.objects.get(id=bookid)
    return render(request, "updatebook.html", {"book": book})


def changebookinfo(request):
    # 更新书籍信息
    bookid = request.GET.get("bookid")
    name = request.GET.get("name")
    price = request.GET.get("price")
    isbn_no = request.GET.get("isbn_no")
    # 第一个种更改方式  取出来改
    book = Book.objects.get(id=bookid)
    book.name = name
    book.price = price
    book.isbn_no = isbn_no
    book.save()

    # Book.objects.update_or_create(name=name, price=price, isbn_no=isbn_no)
    # 查询与新增 合二为一
    # Book.objects.get_or_create(name=name, price=price, isbn_no=isbn_no)# 新增数据进行去重
    # Book.objects.create(name=name, price=price, isbn_no=isbn_no)

    # book = Book()
    # book.name=name
    # book.price=price
    # book.isbn_no=isbn_no
    # book.save()

    return redirect("showbooks")


def testfunction(request):
    # 反向查询
    # bookname = Author.objects.get(id=1).book_set.all()[0].name
    # print(bookname)
    # book = Book(name="人工智能",price=10.01,isbn_no="123454321",enterperson="xx",author_id=3)
    # book.save()

    # 正向查询
    name = Book.objects.get(id=46).author.name
    print(name)

    # book = Book.objects.first()
    # book = Book.objects.last()
    # 函数的注释写的很好 查询出来的结果都 更新某些属性
    # book = Book.objects.filter(id=1).update(name="122")
    # print(book.objects)
    # print(type(book.objects))
    # d = {"name":"三毛流浪记","isbn_no":12233,"price":111.1}
    # Book.objects.create(**d)

    # Book.objects.update_or_create(name="122", price=0, isbn_no="0", defaults={"name": 1})
    # Book.objects.filter(enterperson=None).update(enterperson="lcq")
    # Book.objects.filter(enterperson="").update(enterperson="lcq")
    # print(s)
    # print(type(s))
    # book = Book()
    # book.name = "book1"
    # book.price = 1.1
    # book.isbn_no = "1111"
    # book1 = Book()
    # book1.name = "book2"
    # book1.price = 1.12
    # book1.isbn_no = "11112"
    #
    # Book.objects.bulk_create([book,book1])
    # book = Book.objects.get(name="fff")
    # book.name="西游记"
    # book.save()

    # Book.objects.filter(name="ttt").update(name="三国演义")
    # books = Book.objects.raw("select * from book where name ='三国演义'")
    # books = Book.objects.raw("select * from book where name like '%国%'")
    # for book in books:
    #     print(book.name)
    #     print(book.enterperson)
    # list1 = [1, 2, 3, 4, 5, 6, 7, 8]
    # list1 = Book.objects.all()
    # p = Paginator(list1, 5)
    # print(p.count)  # 总共的数据量大小
    # print(p.object_list) # 被分页的那个对象
    # print(p.per_page) # 每页数据量
    # print(p.num_pages) # 分多少页

    # 如何获取某一页
    # page1 = p.get_page(2)
    # print(page1.number)
    # # print(res.page_range)
    # print(page1.has_next())
    # print(page1.has_previous())
    # print(page1.has_other_pages())
    # print(page1.object_list)

    return HttpResponse("--------")


def showimg(request):
    return render(request, "img.html")


def querybook(request):
    keyword = request.GET.get("keyword")
    # raw  原生  QuerySet  三国演义
    books = Book.objects.raw("select * from book where name like '%{}%'".format(keyword))
    # print(type(books))
    # r"abc\nkk"
    print(dir(books))

    # todo
    return render(request, "result.html", {"books": books})
